// by Jens Schmidt 2009
// for: YOU
// use, modify, copy this as you like

function QueueAssistant() {
	this.uploadQueue = new UploadQueue(this.dbCallback.bind(this));
}

QueueAssistant.prototype.dbCallback = function(state){
	if (state && state != 'fail') {
		this.listWgtModel.items=this.uploadQueue.list;
		this.controller.modelChanged(this.listWgtModel, this);
		if (this.uploadQueue.list.length>0){
			// seems to be ignored?? this.cmdMenuModel.visible=true;
			this.cmdMenuModel.items[1].items[0] = {
				label: $L('Upload All'),
				command: 'upload'
			};		
		}else{
			//this.cmdMenuModel.visible=false;
			this.cmdMenuModel.items[1].items[0] = {};
		}
		this.controller.modelChanged(this.cmdMenuModel, this);
		//Mojo.Controller.errorDialog('menu on');
	}
}

QueueAssistant.prototype.setup = function() {
	
    this.controller.setupWidget("listWgt",
         {
            itemTemplate:"queue/queueItemTemplate", 
            listTemplate:"queue/queueTemplate", 
            swipeToDelete:true, 
            renderLimit: 40,
            reorderable:false
        },
      	this.listWgtModel = {items: this.uploadQueue.list}
	);	

	this.showDetailsHandler = this.showDetails.bindAsEventListener(this);
    this.controller.listen("listWgt", Mojo.Event.listTap,
        this.showDetailsHandler);
		
    this.listDeleteHandler = this.listDelete.bindAsEventListener(this);
    this.controller.listen("listWgt", Mojo.Event.listDelete,
        this.listDeleteHandler);
   	
	this.controller.setupWidget(Mojo.Menu.appMenu,
       	this.attributes = {
                omitDefaultItems: true
            },
    	this.model = {
        visible: true,
        items: []
		});
		
	this.cmdMenuModel = {
		visible: true,
		items: [{},
			{
			items: [
				{}
			]
			},
			{}
		]
	};
	this.controller.setupWidget(Mojo.Menu.commandMenu, undefined, this.cmdMenuModel);	
}

QueueAssistant.prototype.activate = function(event) {
	this.uploadQueue.reload();
}

QueueAssistant.prototype.handleCommand = function(event) {
		if  (event.type==Mojo.Event.command && event.command=='upload'){
			// we let the main screen handle this
			this.controller.stageController.popScene({
				'action': 'uploadAll'
			});
		}
}

QueueAssistant.prototype.deactivate = function(event) {
}

QueueAssistant.prototype.cleanup = function(event) {
}

QueueAssistant.prototype.listDelete = function(event){
	var deleteIndex = this.uploadQueue.list.indexOf(event.item);
	var msg=this.uploadQueue.deleteElement(deleteIndex);
 }
 
QueueAssistant.prototype.showDetails = function(event){
	this.controller.stageController.pushScene("image-details",{
		'filename': event.item.filename
	})
}